﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Soul.XLS
{
    public class ExcelColumnCollection : List<ExcelColumn>
    {
        public string Title { get; set; }

        public int? TitleRowHeilght { get; set; }

        public ExcelColumnCollection()
        {

        }

        public ExcelColumnCollection(string title)
        {
            Title = title;
        }

        public ExcelColumnCollection(string title, Action<ExcelCellStyle> titleStyle)
        {
            Title = title;
            TitleStyle = titleStyle;
        }

        public ExcelColumnCollection(IEnumerable<ExcelColumn> columns)
        {
            AddRange(columns);
        }

        public int Depth
        {
            get
            {
                return new ExcelColumn(this).GetDepth();
            }
        }

        public void CalcSpan()
        {
            new ExcelColumn(this).CalcSpan();
        }

        public ExcelColumnCollection Columns
        {
            get
            {
                var columns = new ExcelColumn(this).GetLeafNodes();
                if (columns.Count != columns.Select(s => s.Code).Distinct().Count())
                {
                    throw new InvalidOperationException("叶子节点的code不能重复");
                }
                return new ExcelColumnCollection(columns);
            }
        }

        public Action<ExcelCellStyle> TitleStyle { get; set; }

        internal ExcelCellStyle ApplyTitleStyle(ExcelCellStyle style)
        {
            TitleStyle?.Invoke(style);
            return style;
        }
    }
}
